package com.xunyu.shopping_admin_service.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinyu.shopping_common.entity.AdminRole;
import com.xinyu.shopping_common.entity.Role;
import com.xinyu.shopping_common.entity.RolePermission;
import com.xinyu.shopping_common.enums.CodeEnum;
import com.xinyu.shopping_common.exception.BusException;
import com.xinyu.shopping_common.service.IRoleService;
import com.xunyu.shopping_admin_service.mapper.AdminRoleMapper;
import com.xunyu.shopping_admin_service.mapper.RoleMapper;
import com.xunyu.shopping_admin_service.mapper.RolePermissionMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@DubboService
@Transactional
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private AdminRoleMapper adminRoleMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public void add(Role role) {
        roleMapper.insert(role);
    }

    @Override
    public void update(Role role) {
        roleMapper.updateById(role);
    }

    @Override
    public void delete(Long rid) {
        //0.判断角色是否关联有用户（管理员），如果有关联不能删除
        LambdaQueryWrapper<AdminRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(AdminRole::getRid, rid);

        Long count = adminRoleMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new BusException(CodeEnum.ADMIN_HAS_ROLE_ERROR);
        }

        //1.先删除角色权限中间表数据
        LambdaQueryWrapper<RolePermission> rolePermissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        rolePermissionLambdaQueryWrapper.eq(RolePermission::getRid, rid);

        rolePermissionMapper.delete(rolePermissionLambdaQueryWrapper);


        //2.删除角色表
        roleMapper.deleteById(rid);
    }

    @Override
    public Role findById(Long rid) {
        return roleMapper.findById(rid);
    }

    @Override
    public List<Role> findAll() {
        return roleMapper.selectList(null);
    }

    @Override
    public Page<Role> search(int page, int size) {

        Page<Role> rolePage = new Page<>(page, size);

        roleMapper.selectPage(rolePage, null);

        return rolePage;
    }

    @Override
    public void updatePermissionToRole(Long rid, Long[] pids) {

        //1.删除角色权限中间表的所有当前rid对应的数据 t_role_permission
        LambdaQueryWrapper<RolePermission> rolePermissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        rolePermissionLambdaQueryWrapper.eq(RolePermission::getRid, rid);
        rolePermissionMapper.delete(rolePermissionLambdaQueryWrapper);

        //2.插入新的数据
        for (Long pid : pids) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setPid(pid);
            rolePermission.setRid(rid);

            rolePermissionMapper.insert(rolePermission);
        }
    }
}
